package com.marketingcenter.controller;

import com.marketingcenter.entity.Coupon;
import com.marketingcenter.service.CouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 优惠券控制器
 */
@RestController
@RequestMapping("/coupons")
public class CouponController {

    @Autowired
    private CouponService couponService;

    /**
     * 创建优惠券
     */
    @PostMapping
    public ResponseEntity<Coupon> create(@RequestBody Coupon coupon) {
        coupon.setType(Coupon.PromotionType.COUPON);
        Coupon createdCoupon = couponService.create(coupon);
        return ResponseEntity.ok(createdCoupon);
    }

    /**
     * 更新优惠券
     */
    @PutMapping("/{id}")
    public ResponseEntity<Coupon> update(@PathVariable Long id, @RequestBody Coupon coupon) {
        coupon.setId(id);
        Coupon updatedCoupon = couponService.update(coupon);
        return ResponseEntity.ok(updatedCoupon);
    }

    /**
     * 删除优惠券
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> delete(@PathVariable Long id) {
        couponService.delete(id);
        return ResponseEntity.noContent().build();
    }

    /**
     * 根据ID查询优惠券
     */
    @GetMapping("/{id}")
    public ResponseEntity<Coupon> getById(@PathVariable Long id) {
        Coupon coupon = couponService.findById(id);
        return coupon != null ? ResponseEntity.ok(coupon) : ResponseEntity.notFound().build();
    }

    /**
     * 查询所有优惠券
     */
    @GetMapping
    public ResponseEntity<List<Coupon>> getAll() {
        List<Coupon> coupons = couponService.findAll();
        return ResponseEntity.ok(coupons);
    }

    /**
     * 根据状态查询优惠券
     */
    @GetMapping("/status/{status}")
    public ResponseEntity<List<Coupon>> getByStatus(@PathVariable Integer status) {
        List<Coupon> coupons = couponService.findByStatus(status);
        return ResponseEntity.ok(coupons);
    }

    /**
     * 查询活动中的优惠券
     */
    @GetMapping("/active")
    public ResponseEntity<List<Coupon>> getActive() {
        List<Coupon> coupons = couponService.findActivePromotions();
        return ResponseEntity.ok(coupons);
    }

    /**
     * 启用优惠券
     */
    @PutMapping("/{id}/enable")
    public ResponseEntity<Void> enable(@PathVariable Long id) {
        couponService.enable(id);
        return ResponseEntity.noContent().build();
    }

    /**
     * 停用优惠券
     */
    @PutMapping("/{id}/disable")
    public ResponseEntity<Void> disable(@PathVariable Long id) {
        couponService.disable(id);
        return ResponseEntity.noContent().build();
    }

    /**
     * 领取优惠券
     */
    @PostMapping("/{id}/receive")
    public ResponseEntity<Boolean> receive(@PathVariable Long id, @RequestParam Long userId) {
        boolean success = couponService.distributeCoupon(id, userId);
        return ResponseEntity.ok(success);
    }

    /**
     * 使用优惠券
     */
    @PostMapping("/{id}/use")
    public ResponseEntity<Boolean> useCoupon(@PathVariable Long id, @RequestParam Long userId, @RequestParam Long orderId) {
        boolean success = couponService.useCoupon(id, userId, orderId);
        return ResponseEntity.ok(success);
    }

}